package com.ball.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ball.entity.User;
import com.ball.service.LogService;
import com.ball.service.SimpleService;
import com.ball.service.UserService;
import com.ball.service.WalletService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/fabric")
public class FabricController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    WalletService walletService;
    @Autowired
    private LogService logService;
    @Autowired
    private UserService userService;
    @Resource
    private SimpleService simpleService;

    @RequestMapping("")
    public String fabric() {
        return "fabric";
    }
    //开户
    @RequestMapping(value = "/createAccount", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public String createAccount(HttpServletRequest request) {
        JSONObject json=new JSONObject();
        User user=(User)request.getSession().getAttribute("user");
        String mail=user.getMail();
        JSONArray arrayJson =new JSONArray();
        arrayJson.add(0,mail);
        json.put("array",arrayJson);
        json.put("orgId","1");
        json.put("fcn","createAccount");
        json.put("type","invoke");
        return simpleService.chainCode(json);
    }
    //查询所有的币
    @RequestMapping(value = "/balanceAll", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public Map<String, Object>  balanceAll(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<String, Object>();
        JSONObject json=new JSONObject();
        User user=(User)request.getSession().getAttribute("user");
        String mail=user.getMail();
        JSONArray arrayJson =new JSONArray();
        arrayJson.add(0,mail);
        json.put("array",arrayJson);
        json.put("orgId","1");
        json.put("fcn","balanceAll");
        json.put("type","query");
        String res=simpleService.chainCode(json);
        json.put("log", res);
        logService.add(json);
        logger.info(res);
        result.put("result", "1");
        JSONObject jsonObject = JSONObject.parseObject(res);
        String balance=JSONObject.parseObject(jsonObject.getJSONObject("result").toJSONString()).toString();
        result.put("balance",balance);
        logger.info(balance);
        return result;

    }

    @RequestMapping(value = "/transferToken", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public String transferToken(@RequestBody Map<String, Object> map,HttpServletRequest request) {
        JSONObject json=new JSONObject(map);
        User user=(User)request.getSession().getAttribute("user");
        String mail=user.getMail();
        JSONArray arrayJson =new JSONArray();
        arrayJson.add(0,mail);
        arrayJson.add(1,json.get("address"));
        arrayJson.add(2,json.get("tokenName"));
        arrayJson.add(3,json.get("pay"));
        json.put("array",arrayJson);
        json.put("orgId","1");
        json.put("fcn","transferToken");
        json.put("type","invoke");
        System.out.println((map.toString()));
        return simpleService.chainCode(json);
    }


    //    /**
//     *
//     * @param
//     *  //map含有name的ajax需要Json格式
//     * @return
//     */
//    @RequestMapping(value = "/reflashMoney", method = RequestMethod.POST, produces = "application/json")
//    @ResponseBody
//    public Map<String, Object> reflashMoney(@RequestBody Map<String, Object> map) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        JSONObject json = new JSONObject(map);
//        String username = json.getString("username");
//        logger.info(username);
//        User user = userService.queryUserByUsername(json);
//        String address = user.getWalletAddress();
//        logger.info(address);
//        if (isNull(address) || address.equals("null")) {
//
//            do{
//
//            }while (isNull(list1.get(1)));
//            logger.info(list1.toString());
//            json.put("log", list1.get(1));
//            logService.add(json);
//            String  str=list1.get(1);
//            address = str.substring(str.indexOf("INFO")+8, str.indexOf("       "));
//            user.setWalletAddress(address);
//            userService.update(user);
//        }
//        List<String> list = walletService.balance(json);
////            int i = 0;
////            do {
////                Thread.sleep(1000);
////                i++;
////            } while (isNull(list.get(1)) != true && i < 10);
//        do{
//
//        }while (isNull(list.get(1)));
//        String money = list.get(1);
//        logger.info(money);
//        json.put("log", money);
//        logService.add(json);
//        money = money.substring(money.indexOf("balance") + 9, money.indexOf(","));
//        user.setMoney(money);
//        userService.update(user);
//        logger.info(money);
//        result.put("result", "1");
//        result.put("balance", money);
//        result.put("address", user.getWalletAddress());
//        return result;
//
//    }
//
//    @RequestMapping(value = "/transfer", method = RequestMethod.POST, produces = "application/json")
//    @ResponseBody
//    public Map<String, Object> transfer(@RequestBody Map<String, Object> map) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        JSONObject json = new JSONObject(map);
//        String address = json.getString("address");
//        String username = json.getString("username");
//        String pay = json.getString("pay");
//        logger.info(username + pay + address);
//        if (Double.valueOf(pay) > 0.001 && (!address.isEmpty())) {
//            List<String> list = walletService.transfer(json);
////            int i = 0;
////            do {
////                try {
////
////                    Thread.sleep(1000);
////                } catch (InterruptedException e) {
////                    e.printStackTrace();
////                }
////                i++;
////            } while (isNull(list.get(1)) && i < 10);
//            do{
//
//            }while (isNull(list.get(1)));
//            logger.info(list.get(1));
//            json.put("log", list.get(1));
//            logService.add(json);
//            result.put("result", "1");
//        }
//        result.put("result", "1");
//        return result;
//    }

}
